package service

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

type Timer struct {
	Name     string
	StartCmd string
	StopCmd  string
	StartDir string
	Interval time.Duration
}

var TimerMap = make(map[string]*Timer)

func loadTimers() {
	for _, app := range Conf.Client.Timers {
		appSlice := strings.Split(app, ",")
		if len(appSlice) != 5 {
			appSlice = strings.Split(app, Conf.Client.Sep)
			if len(appSlice) != 5 {
				Logger.Fatal("Timer配置不合法")
			}
		}

		itv, _ := strconv.Atoi(appSlice[4])
		if itv == 0 {
			Logger.Fatal("timer间隔不能为0")
		}

		d := &Timer{
			Name:     appSlice[0],
			StartCmd: appSlice[1],
			StopCmd:  appSlice[2],
			StartDir: appSlice[3],
			Interval: time.Duration(itv) * time.Second,
		}

		TimerMap[d.Name] = d
	}

	for _, v := range TimerMap {
		fmt.Printf("即将监控的timer:%#v\n", v)
	}
}
